package chapter4.parallel;

import com.rabbitmq.client.*;
import util.Conn;

import java.io.IOException;

/**
 * Created by uu on 2017/6/21.
 */
public class resize_picture_consumer {
    private static boolean autoAck = true; // 是否默认Ack
    public static void main(String[] args) throws Exception{
        Connection conn = new Conn().getConnection();
        final Channel channel= conn.createChannel();
        channel.exchangeDeclare("upload-pictures","fanout",true);
        channel.queueDeclare("resize-picture",true,false,false,null);
        channel.queueBind("resize-picture","upload-pictures","");
        channel.basicConsume("resize-picture",autoAck,"consumer_tag",
                new DefaultConsumer(channel) {
                    @Override
                    public void handleDelivery(String consumerTag,
                                               Envelope envelope,
                                               AMQP.BasicProperties properties,
                                               byte[] body)
                            throws IOException
                    {
                        String routingKey = envelope.getRoutingKey();
                        String contentType = properties.getContentType();
                        long deliveryTag = envelope.getDeliveryTag();
                        // (process the message components here ...)
                        System.out.println(contentType);
                        System.out.println(new String(body,"UTF-8"));
                        channel.basicAck(deliveryTag, false);
                    }
                });
        System.out.println("resize-picture start consume!");
    }

}
